$(function() {
	//设置活动按钮
	changeHeaderMenu(0);
	changeMenu(0, 0);
	//初始化列表数据
	initTable();
	savehandle();
});
function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : refresh
	});
}

/**
 * 初始化表格数据
 */
function initTable() {
	$('#grid').grid({
		title : '',
		pager : true,
		tbar : '#navbar',
		store : {
			url : '/fw/agent/agentDev_getlist.action',
			idProperty : 'agentDevIndex',
			uploadPagesize : 'rows'
		},
		pageCfg : {
			btnCount : 5
		},
		columns : [
		        {cn:'网吧/企业名称', en:'name', width : 200},
		        {cn:'设备SN', en:'sn', width : 150, tpl: function(r) {
		        	if(r.fwsn)
		        		return r.fwsn;
		        	return r.sn;
		        }},
		        {cn:'设备版本号', en:'sysVer',width : 100},
		        {cn:'设备到期时间', en:'expireTime',width : 200, tpl: function(r) {
		        	var time = r.expireTime;
		        	if(time) {
			        	var a = timeSetRed(time);
			        	var html = '<span>'+time+'</span>';
			        	if(a == "1"){
			        		html = '<span style="color: red;">'+time+'</span>';
			        	}
			        	return html;
		        	}
		        }},
		        {cn:'状态', en:'activeLast',width : 80, tpl: function(r) {
		        	var date  = new Date().getTime();
		        	var activeLast = r.activeLast;
		        	if(activeLast != null && activeLast != ""){
			        	var time_m = parseInt((date - activeLast.time)/60000);
			        	if(time_m > 70)
			        		return '<span style="color: red;">不活跃</span>';
			        	else
			        		return '<span>活跃</span>';
		        	}
		        	return '<span>暂无数据</span>';
		        }},
		        {cn:'操作', en:'agentDevIndex', width : 200, tpl: function(r) {
		        	var html = '';
		        	html += '<button class="grid_btn qing" onclick="auzList(\'' + r.sn + '\',\'' + (r.fwsn?r.fwsn:'') + '\')">详情</button>';
		        	html += '&nbsp;&nbsp;&nbsp;&nbsp;';
		        	html += '<button class="grid_btn huang" onclick="consume(\'' + r.sn + '\')">续时</button>';
		        	html += '&nbsp;&nbsp;&nbsp;&nbsp;';
	        		html += '<button class="grid_btn hong" onclick="btn_del(' + r.agentDevIndex + ')">删除</button>';
	        		return html;
		        }}
				]
	});
}
function timeSetRed(time) {
	var s = new Date().getTime();
	time = time.replace(new RegExp("-","gm"),"/");
    var haomiaoTime = (new Date(time)).getTime();
    var t = haomiaoTime - s;
    if(t<1728000000){
    	return "1";
    }else {
    	return "0";
    }
}
function auzList(sn, fwsn){
	if(fwsn)
		sn += "&fwsn=" + fwsn;
	location.href = "auzList.jsp?sn=" + sn;
}

function consume(sn) {
	location.href = "consume.jsp?sn=" + sn;
}

function addInCafe() {
	location.href = "inCafeAdd.jsp";
}

function btn_del(id) {
	if(confirm("确定要删除数据？")){
		$.getJSON(
			'/fw/agent/agentDev_delInCafe.action',
			{
				'ids': id
			},
			function(data){
				refresh();
			}
		);
	}
}

function del(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要删除的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids.join(',');
		if(confirm("这些数据确定要删除数据？")){
			$.getJSON(
				'/fw/agent/agent_del.action',
				{
					'ids':idsStr
				},
				function(data){
					$('#alertdiv').savehandle('handle',data,2000);
					refresh();
				}
			);
		}
	}
}
function findByName() {
	var name = $('#f_name').val();
	$('#grid').grid('load',{
		data:{
			'name': name,
		}
	});
}
//获取当前操作网吧/企业信息
function agentConsump() {
	var time = $('#add_time').val();//点数
	if(!time) {
		$('#alertdiv').savehandle('setmsg',{msg:'请输入你要续时的天数!',timer:2000});
		return;
	}
	var ids = $('#grid').grid('getSelectedIds');
	var idsStr;
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择你要续时的网吧!',timer:2000});
		return;
	}else if(ids.length > 1) {
		$('#alertdiv').savehandle('setmsg',{msg:'你只能选择一个网吧进行续时!',timer:2000});
		return;
	}else{
	idsStr = ids.join(',');
	$.getJSON(
			'/fw/agent/agentDev_findbyId.action',
			{
				'agentDevIndex':idsStr
			},
			function(data){
				data = data.result.rows[0];
				findByPoint(data);
				
			}
		);
	}
}
//查询Point
function findByPoint(agent) {
	var agentId = agent.agentIndex;
	$.getJSON(
			'/fw/agent/agentReg_findById.action',
			{
				'agentIndex':agentId
			},
			function(data){
				data = data.result.rows[0];
				consump(data,agent.agentDevIndex);
			}
		);
}
//续时
function consump(data,agentDevIndex) {
	var time = $('#add_time').val();
    time = parseInt(time);
	var result = 1;
	if(data.agentPoint < time||data.agentPoint == 0 ){
		result = 2;
	}
	var ids = $('#grid').grid('getSelectedIds');
	var agentDevIndex = ids.join(',');
	    var data;
	    	data = {
	    			'ifwAgentRegInfo.agentIndex':data.agentIndex,
	    			'ifwAgentDevInfo.agentDevIndex':agentDevIndex,
				    'point':time,
				    'time':time,
				    'result':result,
				    'type':1,
				    }
	    $.getJSON('/fw/agent/agentCon_addConsume.action',
				  data,function(data){
					if(data.success){
						if(data.result == "1"){
							$('#alertdiv').savehandle('setmsg',{msg:'充值成功',timer:5000});
						}else {
							$('#alertdiv').savehandle('setmsg',{msg:'充值失败,请确保剩余点数足够',timer:5000});
						}
					}else{
						alert("操作失败,请稍候重试");
					}
				}
			);
	
}

//购买点卡
function byCard() {
	var time = $('#add_card').val();
	if(!time){
		$('#alertdiv').savehandle('setmsg',{msg:'请输入购买点卡的天数',timer:2000});
	}
        time = parseInt(time);
    data = {
	    'point':time,
	    'time':time,
	    'type':2,
	       }
  $.getJSON(
			'/fw/agent/agentCon_addCard.action',
			  data,function(data){
				if(data.success){
					if(data.result == "1"){
						$('#alertdiv').savehandle('setmsg',{msg:'购买成功',timer:5000});
					}else {
						$('#alertdiv').savehandle('setmsg',{msg:'购买失败,请确保剩余点数足够',timer:5000});
					}
				}else{
					alert("操作失败,请稍候重试");
				}
			}
		);
}
/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}